$(document).ready(function() {
    $( "#from" ).datepicker({
      defaultDate: "-1M",
	  dateFormat: 'dd/mm/yy',
      changeMonth: true,
      changeYear: true,
		maxDate: 0,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      changeMonth: true,
	  dateFormat: 'dd/mm/yy',
      changeYear: true,
	  maxDate: 0,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
	$('#myModal').modal();
	$( "#from_modal" ).datepicker({
      defaultDate: "-1M",
      changeMonth: true,
	  dateFormat: 'dd/mm/yy',
      changeYear: true,
		maxDate: 0,
      onClose: function( selectedDate ) {
        $( "#to_modal" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to_modal" ).datepicker({
      changeMonth: true,
	  dateFormat: 'dd/mm/yy',
      changeYear: true,
	  maxDate: 0,
      onClose: function( selectedDate ) {
        $( "#from_modal" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
	
	$("#establecermodal").click(function(){
		if($("#from_modal").val()=="" || $("#to_modal").val()=="")
		{
			$('#alerta').show();
		}
		else
		{
			$("#from").val($("#from_modal").val());
			$("#to").val($("#to_modal").val());
			$("#seleccion").val($("#seleccion_modal").val());
			$('#myModal').modal('hide');
			generarReporte();
		}
	});
	
  $("#Grid")
			.jqGrid(
					{
						url : '/getReporte4?from='
							+ $('#from').val() + '&to='
							+ $('#to').val() + '&selec='
							+ $('#seleccion').val(),
						datatype : 'json',
						mtype : 'GET',
						loadonce : true,
						ajaxGridOptions : {
							contentType : 'application/json; charset=utf-8'
						},
						defaults : {
									recordtext : "Páh. {0} - {1} de {2}",
									emptyrecords : "No existen datos",
									loadtext : "Cargando...",
									pgtext : "Pág {0} de {1}"
								},
						colNames : [ 'Proyecto',
								'Nombre del cliente','Monto ($)','Nivel de Avance (%)' ],
						colModel : [ {
							name : 'proyecto',
							label : 'Proyecto',
							width : 125
						}, {
							name : 'nomcliente',
							label : 'Nombre del cliente',
							width : 180
						}, {
							name : 'monto',
							label : 'Monto ($)',
							width : 70
						}, {
							name : 'avance',
							label : 'Nivel de Avance (%)',
							width : 125
						} ],
						pager : "#Pager",
						rowNum : 10,
						rowList : [ 10, 20 ],
						sortname : 'name',
						sortorder : "asc",
						viewrecords : true,
						gridview : true,
						width : 850,
						height : 231,
						rownumbers : true,
						caption : 'AVANCE DE LOS X PROYECTOS CON MAYORES INGRESOS',
						footerrow : true
	
					})

	jQuery("#Grid").jqGrid('navGrid', '#Pager', {
		edit : false,
		add : false,
		del : false,
		search : false
	});
	
});

function generarReporte() {
	var grid = $("#Grid");
	var url = '/getReporte4?from='+ $('#from').val() + '&to='+ $('#to').val() + '&selec='+ $('#seleccion').val();
	
	$("#Grid").jqGrid('setGridParam', {
		url : url
	});
	
	$("#Grid").setGridParam({
		datatype : 'json',
		page : 1
	}).trigger('reloadGrid');
	
	return false;
			
}

function restablecer() {
	
	$('#from').val("");
	$('#to').val("");
	$('#seleccion').val(0);
	generarReporte();
	
	return false;
}

function comeindex() {
	window.open('/index','_self',false);
}